package com.iengoo.spacewar;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-12-26 14:52
 */
public class MineContainer {
    private static List<Mine> mineList = new ArrayList<>();

    public volatile static MineContainer instance;

    private MineContainer() {
    }

    public static MineContainer getInstance() {
        if (instance == null) {
            synchronized (MineContainer.class) {
                // 请注意为什么要放到if中去创建对象，而不是直接创建
                // instance = new LevelManager(craft);
                if (instance == null) {
                    instance = new MineContainer();
                }
            }
        }

        return instance;
    }

    public void add(Mine m) {
        synchronized (mineList) {  // 调用notify 的地方 锁对象必须存在
            mineList.add(m);
        }
    }

    public void empty() {
        synchronized (mineList) {
            mineList.removeAll(mineList);
        }
    }

    public void remove(Mine mine) {
        synchronized (mineList) {
            mineList.remove(mine);
        }
    }

    public int size() {
        synchronized (mineList) {
            return mineList.size();
        }
    }

    public Mine get(int index) {
        synchronized (mineList) {
            return mineList.get(index);
        }
    }
}
